import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { STColumn, STComponent } from '@delon/abc/table';
import { SFSchema, SFComponent, SFTreeSelectWidgetSchema } from '@delon/form';
import {HttpService,SupDic,GridComponent} from 'lbf'
import { NzMessageService} from 'ng-zorro-antd';
import { delay } from 'rxjs/operators';
import { of } from 'rxjs';

@Component({
  selector: 'app-dzda-pzmu',
  templateUrl: './pzmu.component.html',
  styleUrls: ['./pzmu.component.less']
})
export class DzdaPzmuComponent implements OnInit {
  param = {sname: 'dzda_pzmucx',queryparas: {}}

  bbParam = { sname: 'query_rep01', form: {} };

  modalBqTitle:string;
  isAddVisible = false;
  isUpVisible = false;
  isBqVisible = false;
  isBbVisible = false;

  enum1: any;
  tags = [];
  inputVisible = false;
  inputValue = '';
  DAH001: number;

  searchSchema: SFSchema = {
    properties: {
        DAH002: {
        type: 'string',
        title: '凭证名称',
        ui: {
          grid:{span:6}
        }
      },
      DGB010: {
        type: 'string',
        title: '所属部门',
        enum: this.supdic.getSFDic('DGB010'),
        ui: {
          widget: 'select',
          allowClear: true,
          grid: {span: 6}
        }
      },
      DAD130: {
        type: 'string',
        title: '凭证标签',
        enum: this.supdic.getSFDic('PZBQ'),
        ui: {
          widget: 'select',
          mode: 'tags',
          allowClear: true,
          grid: {span: 12}
        }
      }
    },
    ui: {
      spanLabelFixed:100,
      grid: {span:8}
    }
  };

  addSchema: SFSchema = {
    properties: {
      DAH002: {
        type: 'string',
        title: '凭证名称'
      },
      DAH003: {
        type: 'string',
        title: '凭证类别',
        enum: this.supdic.getSFDic('DAH003'),
        ui: {
          widget: 'select',
          allowClear: true
        }
      },
      DAH004: {
        type: 'string',
        title: '是否电子化',
        enum: this.supdic.getSFDic('DAH004'),
        ui:{
          widget: 'select',
          allowClear: true
        }
      },
      DAH005: {
        type: 'string',
        title: '是否数据共享',
        enum: this.supdic.getSFDic('DAH005'),
        ui:{
          widget: 'select',
          allowClear: true
        }
      },
      DGB010: {
        type: 'string',
        title: '所属部门',
        enum: this.supdic.getSFDic('DGB010'),
        ui: {
          widget: 'select',
          allowClear: true,
        }
      },
      custom: {
        type: 'string',
        title: '报表名称',
        ui: {
          widget: 'custom'
        }
      },
      REPID :{
        type: 'string',
        title: '报表ID',
        ui: {
          hidden: true
        }
      }
    },
    required: ['DAH002']
  }

  upSchema :SFSchema = {
    properties: {
      DAH001: {
        type: 'number',
        title: '凭证编码',
        ui: {
          hidden: true
        }
      },
      DAH002: {
        type: 'string',
        title: '凭证名称'
      },
      DAH003: {
        type: 'string',
        title: '凭证类别',
        enum: this.supdic.getSFDic('DAH003'),
        ui:{
          widget: 'select',
          allowClear: true
        }
      },
      DAH004: {
        type: 'string',
        title: '是否电子化',
        enum: this.supdic.getSFDic('DAH004'),
        ui:{
          widget: 'select',
          allowClear: true
        }
      },
      DAH005: {
        type: 'string',
        title: '是否数据共享',
        enum: this.supdic.getSFDic('DAH005'),
        ui:{
          widget: 'select',
          allowClear: true
        }
      },
      DGB010: {
        type: 'string',
        title: '所属部门',
        enum: this.supdic.getSFDic('DGB010'),
        ui: {
          widget: 'select',
          allowClear: true,
        }
      }
    },
    required: ['DAH002']
  }

  bbSearchSchema:SFSchema = {
    properties: {
        DAD130: {
          type: 'string',
          title: '报表标签',
          enum: this.supdic.getSFDic('BBBQ'),
          ui: {
            widget: 'select',
            mode: 'tags',
            width: 400,
            allowClear: true
          }
        }
    }
  }

  @ViewChild('sersf',{static: false}) sersf: SFComponent;
  @ViewChild('addSf',{static: false}) addSf: SFComponent;
  @ViewChild('upSf',{static: false}) upSf: SFComponent;
  @ViewChild('st', { static: false }) st: GridComponent;
  @ViewChild('bbst', { static: false }) bbst: GridComponent;
  @ViewChild('bbsf', { static: false }) bbsf: SFComponent;
  @ViewChild('inputElement', { static: false }) inputElement: ElementRef;
  columns: STColumn[] = [
    { title: '凭证编码', index: 'DAH001'},// ,dic: 'DAH001'},
    { title: '凭证名称', index: 'DAH002' },
    { title: '凭证类别',index: 'DAH003',dic: 'DAH003'},
    { title: '是否电子化',index: 'DAH004',dic: 'DAH004'},
    { title: '是否数据共享',index: 'DAH005',dic:'DAH005'},
    { title: '所属部门',index: 'DGB010',dic: 'DGB010'},
    {
      title: '操作区',
      buttons: [
          {
            text: '修改',
            click: (record) => this.update(record)
          },
          {
            text: '删除',
            click: (record) => this.del(record),
            pop: true,
            popTitle: '是否删除？'
          },
          {
            text: '标签维护',
            click: (record) => this.addPzbq(record)
          }
      ]
    }
  ];

  bbColumns: STColumn[] = [
    { title: '报表id', index: 'REPID' },
    { title: '报表版本号', index: 'REPVER', type: 'number' },
    { title: '参数模板', index: 'REPPAR' },
    { title: '报表名称', index: 'REPNAME' },
    { title: '操作员', index: 'AAE011' },
    { title: '操作日期', index: 'AAE036' },
    { title: '报表标签',index: 'DAD130'},
    {
      title: '操作区',
      buttons: [
        {
          text: '添加',
          click: (record: any) => this.bbtj(record),
        },
      ],
    },
  ];

  constructor(private httpservice: HttpService,
              private supdic: SupDic,
              private msgSrv: NzMessageService
    ) { }

  ngOnInit() {
    this.initKsTree1();

   
   }

 // 初始化科室树
 initKsTree1(){
  this.httpservice.lbservice('ksgl_kslist',{}).then(res=>{
    console.log(res.message.list);
    this.enum1 = res.message.list;
  });
}

  add() {
    this.isAddVisible = true
  }

  handleCancel(){
    this.isAddVisible = false;
    this.isUpVisible = false;
    this.isBqVisible = false;
  }  

  bbAdd(){
    this.isBbVisible = true
  }

  handleBbCancel(){
    this.isBbVisible = false;
  }

  bbtj(rec){
    this.addSf.setValue('/REPID',rec.REPID);
    this.addSf.setValue('/custom',rec.REPNAME);
    this.isBbVisible = false;
  }

  saveAdd(data){
    this.isAddVisible = false;
    this.httpservice.lbservice('dzda_pzmuxz',{para: this.addSf.value}).then(resdata => {
      if(resdata < 1){
        this.msgSrv.error(resdata.errmsg);
      }else{
        this.msgSrv.success('新增成功');
        this.st.reload();
      }
    })
  }

  query(){
    this.st.pi = 1
    this.st.reload(this.sersf.value);
    
  }

  bbQuery(){
    this.bbst.reload(this.bbsf.value);
 }
  update(record){
    console.log(record)
    this.isUpVisible = true;
    this.upSf.setValue('/DAH001',record.DAH001);
    this.upSf.setValue('/DAH002',record.DAH002);
    this.upSf.setValue('/DAH003',record.DAH003);
    this.upSf.setValue('/DAH004',record.DAH004);
    this.upSf.setValue('/DAH005',record.DAH005);
  }

  saveUp(){
    this.isUpVisible = false;
    this.httpservice.lbservice('dzda_pzmuxg',{para: this.upSf.value}).then(resdata =>{
      if(resdata.code < 1){
        this.msgSrv.error(resdata.errmsg);
      }else{
        this.msgSrv.success('修改成功');
        this.st.st.reload();
      }
    })
  }

  del(record){
    this.httpservice.lbservice('dzda_pzmusc',{DAH001: record.DAH001}).then(resdata =>{
      if(resdata.code < 1){
        this.msgSrv.error(resdata.errmsg);
      }else{
        this.msgSrv.success('删除成功');
        this.st.st.reload();
      }
    })
  }

  addPzbq(rec){
    this.DAH001 = rec.DAH001;
    this.modalBqTitle = rec.DAH002+ ' 新增标签';
    this.httpservice.lbservice('dzda_getpzbq',{DAH001: rec.DAH001}).then(res => {
      if(res.code >0 ){
        this.tags = res.message.list;
      }
    })

    setTimeout(() => {
      this.isBqVisible = true;
    }, 1000);
  }


  // tags
  handleClose(removedTag: {}): void {
    this.tags = this.tags.filter(tag => tag !== removedTag);
  }

  sliceTagName(tag: string): string {
    const isLongTag = tag.length > 20;
    return isLongTag ? `${tag.slice(0, 20)}...` : tag;
  }

  showInput(): void {
    this.inputVisible = true;
    setTimeout(() => {
      this.inputElement.nativeElement.focus();
    }, 10);
  }

  handleInputConfirm(): void {
    if (this.inputValue && this.tags.indexOf(this.inputValue) === -1) {
      this.tags = [...this.tags, this.inputValue];
    }
    this.inputValue = '';
    this.inputVisible = false;
  }

  savepz(){
    this.httpservice.lbservice('dzda_pzbqxz',{DAH001: this.DAH001,TAGS: this.tags}).then(res => {
      if(res.code >0){
        this.msgSrv.success("凭证标签更改成功")
        this.searchSchema.properties.DAD130.enum = res.message.dic;
        this.sersf.refreshSchema();
      }
    })
    this.isBqVisible = false;
  }

}
